package com.exp.bookhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exp.bookhouse.mapper.BookMapper;
import com.exp.bookhouse.mapper.OrderDetailMapper;
import com.exp.bookhouse.mapper.OrderMapper;
import com.exp.bookhouse.pojo.Order;
import com.exp.bookhouse.pojo.OrderDetail;
import com.exp.bookhouse.pojo.vo.TableVo;
import com.exp.bookhouse.service.OrderService;
import com.exp.bookhouse.util.AmountUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private BookMapper bookMapper;


    /**
     * 保存订单
     * @param order
     */
    @Transactional
    @Override
    public void saveOrder(Order order) throws Exception {
        synchronized(Order.class){
            List<OrderDetail> orderDetails = order.getOrderDetails();
            int count = orderMapper.insert(order);  //插入订单，回显id
            if(count == 0){
                throw new Exception("订单插入失败");
            }

            for (OrderDetail orderDetail : orderDetails) {
                orderDetail.setOrderId(order.getId());
                count = orderDetailMapper.insert(orderDetail);   //保存订单细节
                if(count == 0){
                    throw new Exception("订单细节插入失败");
                }
                //更新库存
                count = bookMapper.updateStock(orderDetail.getBookId(),orderDetail.getBookNum());
                if(count == 0){
                    throw new Exception("库存更新失败");
                }
            }
        }
    }

    /**
     * 分页查询订单信息
     * @param pageNum
     * @param pageSize
     * @param payStatus
     * @param openid
     * @return
     */
    @Override
    public IPage<Order> getAllOrder(Integer pageNum, Integer pageSize, Integer payStatus, String openid) {
        IPage<Order> page = new Page<>(pageNum, pageSize);   //分页
        IPage<Order> iPage = orderMapper.getAllOrder(page,payStatus,openid);
        long total = 0;
        for (Order order : iPage.getRecords()) {
            total += order.getOrderDetails().size();
        }
        iPage.setTotal(total);
        return iPage;
    }

    /**
     * 分页查询订单
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Order> queryOrderByPage(Integer page, Integer size) {
        IPage<Order> iPage = new Page<>(page,size);
        return orderMapper.selectPage(iPage,null);
    }

    /**
     * 查询订单和细节
     * @param id
     * @return
     */
    @Override
    public Order queryOrderDetail(Long id) {
        return orderMapper.queryOrderById(id);
    }

    @Override
    public int queryCountOrder() {
        return orderMapper.selectCount(null);
    }

    @Override
    public String queryOrderTotalPrice() {
        List<Long> prices = orderMapper.queryOrderTotalPrice();
        Long total = 0L;
        for (Long price : prices) {
            total += price;
        }
        return AmountUtil.changeF2Y(total);
    }

    /**
     * 查询本月订单及细节
     * @return
     */
    @Override
    public List<TableVo> queryOrderByMonth() {
        List<TableVo> tableVos =orderMapper.queryOrderByMonth();
        return tableVos;
    }


    /**
     * 查询本月的订单总额
     * @return
     */
    @Override
    public String queryOrderTotalPriceByMonth() {
        Long total = orderMapper.queryOrderTotalPriceByMonth();
        if(total == null){
            return "0";
        }
        return AmountUtil.changeF2Y(total);
    }

    /**
     * 查询本日的订单及细节
     * @return
     */
    @Override
    public List<TableVo> queryOrderByDay() {
        return orderMapper.queryOrderByDay();
    }

    /**
     * 查询本日订单总额
     * @return
     */
    @Override
    public String queryOrderTotalPriceByDay() throws Exception {
        Long total = orderMapper.queryOrderTotalPriceByDay();
        if(total == null){
            return "0";
        }
        return AmountUtil.changeF2Y(total);
    }


    /**
     * 查询本年订单及细节
     * @return
     */
    @Override
    public List<TableVo> queryOrderByYear() {
        return orderMapper.queryOrderByYear();
    }

    /**
     * 查询本年订单及细节
     * @return
     */
    @Override
    public List<TableVo> queryOrderAll() {
        return orderMapper.queryOrderAll();
    }

    /**
     * 查询本年订单总额
     * @return
     */
    @Override
    public String queryOrderTotalPriceByYear() {
        Long total = orderMapper.queryOrderTotalPriceByYear();
        if(total == null){
            return "0";
        }
        return AmountUtil.changeF2Y(total);
    }

    /**
     * 按名称查询订单，不分页，模糊查询
     * @return
     */
    @Override
    public List<Order> getOrderByName(String userName){
        List<Order> order = new ArrayList<>();
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.like("user_name",userName);
        order = orderMapper.selectList(wrapper);
        return order;
    }

    /**
     * 查询全部订单
     * @return
     */
    @Override
    public List<Order> queryAllOrder(){
        return orderMapper.queryAllOrder();
    }

}
